package MyGO.controller;

import MyGO.Form.TrackForm;
import MyGO.dao.ITrackFormDao;
import MyGO.dao.impl.TrackFormImpl;
import MyGO.property.CheckPro;
import MyGO.service.ITrackFormService;
import MyGO.service.impl.TrackFormServiceImpl;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import lombok.Getter;

import java.util.List;

public class Jianchajianchashenqing {
    @FXML
    private AnchorPane now;

    @FXML
    private TableView<TrackForm> biaoge;
    @FXML
    private Label had;
    @FXML
    private Label have;

    @FXML
    private TextField binglihao;

    @FXML
    private Button sure;

    @FXML
    private TextField xingming;
    private static int useNum=0;
    private int unUseNum;
    @Getter
    private static TrackForm user = null;
    private ITrackFormDao trackFormService = new TrackFormImpl();
    private List<TrackForm> trackForms = trackFormService.selectTrackFormByStatus("未检查");

    public static void setUser() {
        user = null;
    }

    public void initialize(){
        biaoge.getColumns().clear();
        unUseNum = trackForms.size();
        had.setText("今日已接诊" + useNum +"名患者");
        have.setText("还有" + unUseNum +"名患者在排队");
        liebiao();
        //实现搜索
        sure.setOnAction(e -> {
            if ((xingming.getText() != null) || (binglihao.getText() != null)) {
                if (xingming.getText() != null) {  //名字搜索
                    for (var temp : trackForms){
                        if (temp.getPatientInfoForm().getName().equals(xingming.getText())) {
                            user = temp;
                        }
                    }
                } else{                               //ID搜索
                    for (var temp : trackForms){
                        if (temp.getPatientInfoForm().getId().equals(binglihao.getText())) {
                            user = temp;
                        }
                    }
                }
            }
            if (user == null){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
                alert.setHeaderText("未找到该患者检查申请信息");
                alert.showAndWait();
            }
            else {
                trackForms.clear();
                trackForms.add(user);
                biaoge.getColumns().clear();
                liebiao();
            }
        });
    }

    private void liebiao() {
        //添加编号列
        TableColumn<TrackForm, Number> indexColumn = new TableColumn<>("#");
        indexColumn.setSortable(false);
        indexColumn.setCellFactory(column -> new TableCell<TrackForm, Number>() {
            @Override
            protected void updateItem(Number item, boolean empty) {
                super.updateItem(item, empty);

                if (empty || getIndex() < 0) {
                    setText(null);
                } else {
                    setText(Integer.toString(getIndex() + 1));
                }
            }
        });

        // 确保编号列是第一列
        biaoge.getColumns().add(0, indexColumn);
        //添加其他列
        TableColumn<TrackForm, String> nameColumn = new TableColumn<>("姓名");
        TableColumn<TrackForm, String> idColumn = new TableColumn<>("病历号");

        nameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
        idColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));

        ObservableList<TrackForm> data = FXCollections.observableArrayList(trackForms);
        biaoge.setItems(data);
        biaoge.getColumns().addAll(nameColumn,idColumn);

        // 设置按钮
        TableColumn<TrackForm, Void> actionCol = new TableColumn<>("操作");

        actionCol.setCellFactory(param -> {
            Button deleteButton = new Button("选中");
            TableCell<TrackForm, Void> cell = new TableCell<TrackForm, Void>() {
                @Override
                protected void updateItem(Void item, boolean empty) {
                    super.updateItem(item, empty);

                    if (empty) {
                        setGraphic(null);
                    } else {
                        setGraphic(deleteButton);
                    }
                }
            };

            deleteButton.setOnAction(event -> {
                // 获取当前行的数据
                user = cell.getTableRow().getItem();
                useNum++;
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setContentText("选择完成，请进行下一步操作");
                now.getChildren().clear();
                alert.showAndWait();
            });
            return cell;
        });
        biaoge.getColumns().add(actionCol);
    }
}
